# 0 leds
# 1 switches
# 2 seven segs
# 3 keypad
# 4 stepper motor

# set cp0 timer so that after a few seconds it loads anyway

addi $t0, $0, 0x48     # isr address
mtc0 $t0, $15          # set ebase to isr address

lui $s0, 0xFFFF        # set base address for leds
addi $s1, $s0, 0x0040  # set base address of the switches controller
addi $s2, $s0, 0x0080  # set base address for seven seg controller
addi $s3, $s0, 0x00C0  # set base address of the hex keypad controller
addi $s4, $s0, 0x0100  # set base address of the stepper motor controller

addi $t0, $0, 0x000F   # turn all seven segs on
lui $a1, 0x00F0        # set mask for write enables and no dots
sw $t0, 0($s2)         # turn all seven segs on
sw $a1, 4($s2)         # write zeros without dots

addi $t0, $0, 4        # only want every four presses
sw $t0, 4($s3)         # set hex keypad interrupt interval

loop: lw $t1, 0($s1)   # load switches
lw $t3, 0($s3)         # load hexkeypad buffer
srlv $t0, $t3, $t1     # shift buffer by amount on switches
sw $t0, 0($s0)         # print out to leds
j loop

isr: mfc0 $13, $t0     # get the cause register
addi $t1, $0, 0x1000   # set keypad interrupt mask
and $t0, $t0, $t1      # mask keypad interrupt bit
bne $t0, $0, keypad    # if keypad, jump to handler
eret                   # otherwise return, nothing else handled

keypad: lw $t3, 0($s3) # load hexkeypad buffer
andi $t2, $t3, 0xFFFF  # get only latest four buttons
add $t2, $t2, $a1      # write buttons to the seven segs
sw $t2, 4($s2)         # write to seven segs
eret
